import Mock from 'mockjs';
import setupMock, {
  successResponseWrap,
  failResponseWrap,
} from '@/utils/setup-mock';

import { MockParams } from '@/types/mock';
import { isLogin } from '@/utils/auth';

setupMock({
  setup() {
    // Mock.XHR.prototype.withCredentials = false;

    // 用户信息
    Mock.mock(new RegExp('/userinfo/info'), () => {
      if (isLogin()) {
        const role = window.localStorage.getItem('userRole') || 'admin';
        return successResponseWrap({
          name: '王立群',
          avatar:
            '//lf1-xgcdn-tos.pstatp.com/obj/vcloud/vadmin/start.8e0e4855ee346a46ccff8ff3e24db27b.png',
          email: 'wangliqun@email.com',
          job: 'frontend',
          jobName: '前端艺术家',
          organization: 'Frontend',
          organizationName: '前端',
          location: 'beijing',
          locationName: '北京',
          introduction: '人潇洒，性温存',
          personalWebsite: 'https://www.arco.design',
          phone: '150****0000',
          registrationDate: '2013-05-10 12:10:00',
          accountId: '15012312300',
          certification: 1,
          role,
        });
      }
      return failResponseWrap(null, '未登录', 50008);
    });

    Mock.mock(new RegExp('/admin/login'), (params:Object)=>{
      return {
        "code": 200,
        "msg": "成功",
        "data": {
            "token": "25e2fc3cd77e49f20b77f3bb78002a6e",
            "admin_info": {
                "admin_id": 1,
                "username": "admin",
                "phone": "13800138000"
            },
            "rules": {
                "生产管理": "/admin/product/index",
                "产品分类管理": "/admin/product_cat/index",
                "产品分类一级": "/admin/product_cat/one",
                "产品分类二级": "/admin/product_cat/second",
                "产品分类列表树": "/admin/product_cat/tree",
                "产品分类新增": "/admin/product_cat/add",
                "产品分类删除": "/admin/product_cat/del",
                "产品分类修改": "/admin/product_cat/edit",
                "机器属性管理": "/admin/machine_attr/index",
                "机器属性列表": "/admin/machine_attr/list",
                "机器属新增": "/admin/machine_attr/add",
                "机器属删除": "/admin/machine_attr/del",
                "机器属修改": "/admin/machine_attr/edit",
                "分类机器属性": "/admin/cat_machine_attr/index",
                "分类机器属性列表": "/admin/cat_machine_attr/list",
                "分类关联机器属性": "/admin/cat_machine_attr/band",
                "分类机器属性删除": "/admin/cat_machine_attr/del",
                "分类电器版本": "/admin/cat_circuit_version/index",
                "分类电器版本列表": "/admin/cat_circuit_version/list",
                "分类电器版本新增": "/admin/cat_circuit_version/add",
                "分类电器版本删除": "/admin/cat_circuit_version/del",
                "分类电器版本下拉": "/admin/cat_circuit_version/pull",
                "分类钣金版本": "/admin/cat_plate_version/index",
                "分类钣金版本列表": "/admin/cat_plate_version/list",
                "分类钣金版本新增": "/admin/cat_plate_version/add",
                "分类钣金版本删除": "/admin/cat_plate_version/del",
                "分类钣金版本下拉": "/admin/cat_plate_version/pull",
                "分类钣金版本模块": "/admin/cat_plate_module/index",
                "分类钣金版本模块列表": "/admin/cat_plate_module/list",
                "分类钣金版本模块新增": "/admin/cat_plate_module/add",
                "分类钣金版本模块删除": "/admin/cat_plate_module/del",
                "分类钣金版本模块零件列表": "/admin/module_part/list",
                "分类钣金版本模块零件新增": "/admin/module_part/add",
                "分类钣金版本模块零件修改数量": "/admin/module_part/modify_number",
                "分类钣金版本模块零件删除": "/admin/module_part/del",
                "零件分类": "/admin/part_cat/index",
                "零件分类树": "/admin/part_cat/tree",
                "零件分类新增": "/admin/part_cat/add",
                "零件分类删除": "/admin/part_cat/del",
                "零件分类编辑": "/admin/part_cat/edit",
                "零件": "/admin/part/index",
                "零件列表": "/admin/part/list",
                "零件新增": "/admin/part/add",
                "加减库存": "/admin/part/modify_stock",
                "零件编辑": "/admin/part/edit",
                "零件删除": "/admin/part/del",
                "零件下拉选择": "/admin/part/pull",
                "部门职位": "/admin/position/index",
                "部门职位列表树": "/admin/position/tree",
                "职位新增": "/admin/position/add",
                "职位权限列表树": "/admin/position/rule_tree",
                "职位绑定权限": "/admin/position/band_rule",
                "职位删除": "/admin/position/del",
                "职位编辑": "/admin/position/edit",
                "后台用户": "/admin/admin/index",
                "用户列表": "/admin/admin/list",
                "用户新增": "/admin/admin/add",
                "用户绑定职位": "/admin/admin/band_position",
                "删除后台用户": "/admin/admin/del",
                "后台用户权限树": "/admin/admin_rule/admin_tree",
                "机器管理": "/admin/product/index",
                "产品（机器）新增": "/admin/product/add",
                "产品（机器）列表": "/admin/product/list",
                "产品（机器）详情": "/admin/product/detail",
                "产品（机器）零件列表": "/admin/product_part/list",
                "产品（机器）零件出库": "/admin/product_part/out_store",
                "机器状态修改": "/admin/product/status_edit",
                "机器设置出厂时间": "/admin/product/leave_time",
                "更新物联网卡流量": "/admin/iot_card/update_flow",
                "产品（机器）删除": "/admin/product/del",
                "工位管理": "/admin/station/index",
                "工位列表": "/admin/station/list",
                "工位新增": "/admin/station/add",
                "工位删除": "/admin/station/del",
                "工位编辑": "/admin/station/edit",
                "工位关联机器": "/admin/station_product/add",
                "删除关联": "/admin/station_product/del"
            }
        }
      }
    })

    // 登录
    Mock.mock(new RegExp('/userinfo/login'), (params: MockParams) => {
      const { username, password } = JSON.parse(params.body);
      if (!username) {
        return failResponseWrap(null, '用户名不能为空', 50000);
      }
      if (!password) {
        return failResponseWrap(null, '密码不能为空', 50000);
      }
      if (username === 'admin' && password === 'admin') {
        window.localStorage.setItem('userRole', 'admin');
        return successResponseWrap({
          token: '12345',
        });
      }
      if (username === 'user' && password === 'user') {
        window.localStorage.setItem('userRole', 'user');
        return successResponseWrap({
          token: '54321',
        });
      }
      return failResponseWrap(null, '账号或者密码错误', 50000);
    });

    // 登出
    Mock.mock(new RegExp('/userinfo/logout'), () => {
      return successResponseWrap(null);
    });

    // 用户的服务端菜单
    Mock.mock(new RegExp('/userinfo/menu'), () => {
      const menuList = [
        {
          path: '/dashboard',
          name: 'dashboard',
          meta: {
            locale: 'menu.server.dashboard',
            requiresAuth: true,
            icon: 'icon-dashboard',
            order: 1,
          },
          children: [
            {
              path: 'workplace',
              name: 'Workplace',
              meta: {
                locale: 'menu.server.workplace',
                requiresAuth: true,
              },
            },
            {
              path: 'https://arco.design',
              name: 'arcoWebsite',
              meta: {
                locale: 'menu.arcoWebsite',
                requiresAuth: true,
              },
            },
          ],
        },
      ];
      return successResponseWrap(menuList);
    });
  },
});
